package com.elock.intf.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.elock.base.core.Filter;
import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.service.impl.BaseServiceImpl;
import com.elock.base.core.util.ConvertUtil;
import com.elock.base.core.util.ExceptionUtil;
import com.elock.entity.CompanyInfo;
import com.elock.entity.Elock;
import com.elock.entity.ElockRealtimeStatus;
import com.elock.intf.dao.IntfElockDao;
import com.elock.intf.service.IntfElockService;
import com.elock.service.CompanyInfoService;
import com.elock.service.ElockRealtimeStatusService;
import com.elock.service.ElockSaleOrgService;
import com.elock.service.SaleOrgBaseService;

/**
 * 锁
 * @author 仁德
 */
@Service("intfElockServiceImpl")
public class IntfElockServiceImpl extends BaseServiceImpl<Elock>
		implements IntfElockService {

	@Resource(name = "intfElockDao")
	private IntfElockDao intfElockDao;
	@Resource(name = "elockRealtimeStatusServiceImpl")
	private ElockRealtimeStatusService elockRealtimeStatusService;
	@Resource(name = "saleOrgBaseServiceImpl")
	private SaleOrgBaseService saleOrgBaseService;
	@Resource(name = "elockSaleOrgServiceImpl")
	private ElockSaleOrgService elockSaleOrgService;
	@Resource(name = "companyInfoServiceImpl")
	private CompanyInfoService companyInfoService;

	@Override
	@Transactional
	public void saveUpdate(String companyCode, Elock elock, String saleOrgSn) {

		if (ConvertUtil.isEmpty(companyCode)) {
			ExceptionUtil.throwServiceException("请传入企业编号");
		}
		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("company_code", companyCode));
		CompanyInfo companyInfo = companyInfoService.find(filters);
		if (companyInfo == null) {
			ExceptionUtil.throwServiceException("不存在企业编号为[" + companyCode + "]的企业");
		}
		String elockNum = elock.getElockNum();
		if (ConvertUtil.isEmpty(elockNum)) {
			ExceptionUtil.throwServiceException("请传入锁编号");
		}
		Elock pElock = intfElockDao.findByElockNum(elockNum);
		if (pElock == null) {
			elock.setCompanyInfoId(companyInfo.getId());
			this.save(elock);

			//锁的实时状态
			ElockRealtimeStatus elockRealtimeStatus = new ElockRealtimeStatus();
			elockRealtimeStatus.setElockId(elock.getId());
			elockRealtimeStatus.setElockNum(elock.getElockNum());
			elockRealtimeStatus.setCompanyInfoId(companyInfo.getId());
			elockRealtimeStatusService.save(elockRealtimeStatus);
		}
		else {
			if (!ConvertUtil.isEmpty(elock.getElockName())) {
				pElock.setElockName(elock.getElockName());
			}
			if (!ConvertUtil.isEmpty(elock.getSecretKey())) {
				pElock.setSecretKey(elock.getSecretKey());
			}
			if (!ConvertUtil.isEmpty(elock.getRemark())) {
				pElock.setRemark(elock.getRemark());
			}
			this.update(pElock);
		}
	}

	@Override
	@Transactional(readOnly = true)
	public Page<Map<String, Object>> findPage(Pageable pageable, Object[] args) {
		return intfElockDao.findPage(pageable, args);
	}

	@Override
	@Transactional(readOnly = true)
	public Elock findByElockNum(String elockNum) {
		return intfElockDao.findByElockNum(elockNum);
	}
}
